package cn.sunshine.user.service.impl;

import cn.sunshine.basic.constant.BaseContants;
import cn.sunshine.basic.dto.RegisterDto;
import cn.sunshine.basic.exception.BusinessException;
import cn.sunshine.basic.utils.MD5Utils;
import cn.sunshine.basic.utils.StrUtils;
import cn.sunshine.user.domain.Logininfo;
import cn.sunshine.user.domain.User;
import cn.sunshine.user.mapper.LogininfoMapper;
import cn.sunshine.user.mapper.UserMapper;
import cn.sunshine.user.service.IUserService;
import cn.sunshine.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Jiang
 * @since 2022-07-06
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private UserMapper userMapper;

    /*
     * @Title: phoneCodeRegister
     * @Description: 设置User对象的数据
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/7/6 21:42
     * @Parameters: [registerDto]
     * @Return cn.sunshine.user.domain.User
     */

    public User RegisterDto2User(RegisterDto registerDto) {
        // 将registerDto中的参数设置到User中
        User user = new User();
        user.setPhone(registerDto.getPhone());
        user.setUsername(registerDto.getPhone());
        // 将明文密码进行盐值密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5 = MD5Utils.encrypByMd5(salt + registerDto.getPassword());
        // 设置加盐加密的密码
        user.setPassword(md5);
        user.setSalt(salt);
        return user;
    }

    /*
     * @Title: user2Logininfo
     * @Description: 设置Logininfo对象数据
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/7/6 21:49
     * @Parameters: [user]
     * @Return cn.sunshine.user.domain.Logininfo
     */
    public Logininfo user2Logininfo(User user) {
        // 初始化Logininfo的方法，将user数据拷贝到Logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user, logininfo);
        // 设置类型为用户
        logininfo.setType(1);
        return logininfo;
    }

    @Override
    public void phoneCodeRegister(RegisterDto registerDto) {

        // 1.参数非空判断。
        if (StringUtils.isEmpty(registerDto.getSmsCodeValue()) || StringUtils.isEmpty(registerDto.getPassword()) || StringUtils.isEmpty(registerDto.getConfirmPassword())) {
            throw new BusinessException("请正确填写参数");
        }
        // 2.两次密码比对
        if (!registerDto.getPassword().equals(registerDto.getConfirmPassword())) {
            throw new BusinessException("两次填写密码不一致，请重新填写");
        }
        // 3.判断注册时的手机号和获取验证码的手机号一致不。
        // 3.1拼接手机验证码的RedisKey
        String smsCodeKey = BaseContants.verifyCodeConstant.BUSINESS_REGISTER_KEY + registerDto.getPhone();
        Object smsCodeValueTmp = redisTemplate.opsForValue().get(smsCodeKey);
        if (smsCodeValueTmp == null) {
            throw new BusinessException("验证码已过期，请重新获取");
        }
        // 4.判断短信验证码是否一致
        String[] split = smsCodeValueTmp.toString().split(":");
        String smaCode = split[1];
        if (!smaCode.equalsIgnoreCase(registerDto.getSmsCodeValue())) {
            throw new BusinessException("验证码错误，请重新输入");

        }
        // 5.将数据保存在两张表中。
        User user = RegisterDto2User(registerDto);
        Logininfo logininfo1 = user2Logininfo(user);
        logininfoMapper.save(logininfo1);
        user.setLogininfoId(logininfo1.getId());
        userMapper.save(user);
    }

    /*
     * @Title: emailCodeRegister
     * @Description: 邮箱注册
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/7/9 18:25
     * @Parameters: [registerDto]
     * @Return void
     */
    @Override
    public void emailCodeRegister(RegisterDto registerDto) {
        // 1.参数非空判断。
        if (StringUtils.isEmpty(registerDto.getConfirmPassword()) || StringUtils.isEmpty(registerDto.getPassword())) {
            throw new BusinessException("请正确填写参数");
        }
        // 2.两次密码比对
        if (!registerDto.getPassword().equals(registerDto.getConfirmPassword())) {
            throw new BusinessException("两次填写密码不一致，请重新填写");
        }
        // 3.将数据保存在两张表中。
        User user = RegisterDto2User(registerDto);
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.sava(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }
}